﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QLChoDiaOc.Process;
using System.Data;

namespace QLChoDiaOc.Forms
{
    /// <summary>
    /// Interaction logic for FormDanhSachNguoiBan.xaml
    /// </summary>
    public partial class FormDanhSachNguoiBan : Page
    {
        ShowInfo _showInfo = new ShowInfo();
        UpdateInfo _updateInfo = new UpdateInfo();
        OtherHandle _otherHandle = new OtherHandle();
        static bool m_bSelectData = false;

        public FormDanhSachNguoiBan()
        {
            InitializeComponent();
            _showInfo.ShowListKhachHang(dataKhachHang);
        }

        bool KiemTraNhap()
        {
            if (hoTenTextBox.Text == "" ||
                ngaySinhDatePicker.SelectedDate == null ||
                diaChiTextBox.Text == "" ||
                emailTextBox.Text == "" ||
                soDTTextBox.Text == "")
                return true;
            else return false;
        }

        void ClearIndex()
        {
            maKHtextbox.Text = "";
            hoTenTextBox.Text = "";
            ngaySinhDatePicker.SelectedDate = null;
            diaChiTextBox.Text = "";
            emailTextBox.Text = "";
            soDTTextBox.Text = "";
            dataKhachHang.ItemsSource = null;
        }

        private void dataKhachHang_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectData)
            {
                var item = dataKhachHang.SelectedItem as KHACHHANG;
                maKHtextbox.Text = item.MaKH;
                hoTenTextBox.Text = item.HoTen;
                ngaySinhDatePicker.SelectedDate = item.NgaySinh.Value;
                diaChiTextBox.Text = item.DiaChi;
                emailTextBox.Text = item.Email;
                soDTTextBox.Text = item.SoDT;
            }
            else
            {
                m_bSelectData = false;
                _showInfo = new ShowInfo();
                _showInfo.ShowListKhachHang(dataKhachHang);
            }
        }

        private void btnSuaKhachHang_Click(object sender, RoutedEventArgs e)
        {
            if (KiemTraNhap())
                MessageBox.Show("Bạn chưa nhập đầy đủ thông tin", "Thông Báo");
            else
            {
                _updateInfo.UpdateKhachHang(maKHtextbox.Text, 
                                            hoTenTextBox.Text, 
                                            ngaySinhDatePicker.SelectedDate.Value, 
                                            diaChiTextBox.Text, 
                                            emailTextBox.Text, 
                                            soDTTextBox.Text);
                if (!m_bSelectData)
                    m_bSelectData = true;
                ClearIndex();
            }
        }

        private void soDTTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _otherHandle.IsNumber(soDTTextBox);
        }

        private void btnTimKiem_Click(object sender, RoutedEventArgs e)
        {
            maKHtextbox.IsEnabled = true;
            btnTimKhachHang.IsEnabled = true;
        }

        private void bntReloadKhachHang_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bSelectData)
                m_bSelectData = true;
            ClearIndex();
            m_bSelectData = false;
            _showInfo = new ShowInfo();
            _showInfo.ShowListKhachHang(dataKhachHang);
        }

        private void btnTimKhachHang_Click(object sender, RoutedEventArgs e)
        {
            _showInfo = new ShowInfo();
            _showInfo.ShowListKhachHang(dataKhachHang, maKHtextbox.Text);
            btnTimKhachHang.IsEnabled = false;
            maKHtextbox.IsEnabled = false;
        }
    }
}
